#include <vector>
#include <ext/hash_map>
#include <deque>
#include <numeric>
#include <algorithm>
#include <list>
#include <string>


#define DEBUG_LEVEL 8
#include <debug_func.h>
#include <print_error.h>

#include <gsl_cdf_hypergeometric_P.h>
#include "get_arg.h"

//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

//	main

//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


int main (int argc, char *argv[])
{
	// we use cout to print progress so flush per call
//	cout.setf(std::ios_base::unitbuf);

	debug_func_cout(5);
//	std::ios::sync_with_stdio(false);

	t_program_args args;
	try
	{
		if (!process_command_line_options(argc, argv, args))
			return 1;
		if (args.less_than)
		{
			//		* Computes the cumulative distribution function for a hypergeometric
			//		* random variable. A hypergeometric random variable X is the number
			//		* of elements of type 0 in a sample of size t, drawn from a population
			//		* of size n1 + n0, in which n1 are of type 1 and n0 are of type 0.
			//		*
			//		* This algorithm computes Pr( X <= k ) by summing the terms from
			//		* the mass function, Pr( X = k ).
			//		/*
			//		* Pr (X <= k)
			//		*/
			//		double
			//		gsl_cdf_hypergeometric_P (const unsigned int k, 
			//					 const unsigned int n0,
			//					 const unsigned int n1,
			//					 const unsigned int t)

			std::cout << gsl_cdf_hypergeometric_P (	args.sample_hits, 
													args.total_hits,
													args.total_pop - args.total_hits,
													args.sample_pop) << std::endl;
		}
		else
		{
			// so that calculates greater than or equal
			if (args.sample_hits > 1)
				--args.sample_hits;
			std::cout << gsl_cdf_hypergeometric_Q (	args.sample_hits, 
													args.total_hits,
													args.total_pop - args.total_hits,
													args.sample_pop) << std::endl;
		}

		args.VERBOSE() << "\n\tAll finished!!\n\n";
	}
	catch(std::exception& e)
	{
		std_print_error(args.CERR(),  e.what());
		return false;
	}
	return 0;
}



